package com.hzhc.config;

import com.hzhc.utils.YmlUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.Map;

/**
 * @ClassName: com.hzhc.config.InitializationConfig
 * @Description:测试无需引入
 * @version 1.0
 * @author: duanhm
 * @Date: 2024/08/10 09:05:02
 * 
 */
@Log4j2
//@Component
public class InitializationConfig implements ApplicationRunner {

	// private static ScheduledExecutorService executorService =
	// Executors.newScheduledThreadPool(1);
	private static String profile;

	@Override
	public void run(ApplicationArguments applicationArguments) throws Exception {
		scheduleUpdateConf();
	}

	private void scheduleUpdateConf() {
		try {
			Map<?, ?> lhm = YmlUtil.loadYaml("application.yml");
			profile = (String) YmlUtil.getValByKey(lhm, "spring.profiles.active");
		} catch (Exception e) {
			log.error("加载配置文件application.yml异常");
		}

		// TODO 开启定时刷新内存中配置文件内容
		log.info("refresh config file start");
		// executorService.scheduleAtFixedRate(com.hzhc.config.InitializationConfig::updateConfVal, 0,
		// 10, TimeUnit.SECONDS);
		log.info("refresh config file end");
	}

	/**
	 * 更新配置文件值
	 */
	private static void updateConfVal() {
		try {
			Map<?, ?> lhm = YmlUtil.loadYaml("application-" + profile + ".yml");

			String sensitiveEnable = YmlUtil.getValByKey(lhm, "sensitive.enable").toString();
			log.info("sensitiveEnable=={}", sensitiveEnable);
			// DynamicMailConfig instance = DynamicMailConfig.getInstance();
			// if (!instance.getDynamicMail().equals(mail)) {
			// instance.setDynamicMail(mail);
			// log.info("实时配置mail更新：" + instance.getDynamicMail());
			// }
		} catch (Exception e) {
			log.error("更新配置文件值异常: ", e);
		}
	}

}
